/**
 * 获取字符长度
 */
const len = (data) => {
    return data.length;
};

/**
 * 获取指定长度的单词
 */
const getWord = (str, l) => {
    if (str.length === l) {
        return str;
    }
    return '';
};

/**
 * 判断单词长度是否等于指定长度
 */
const isLenWord = (str, l) => str.length === l;

/**
 * 获取符合条件的单词list
 * @param {*} oldList 旧数据
 * @param {*} newList 新数据
 * @param {*} str
 * @param {*} l
 */
const getWordList = (oldList, newList, str, l) => {
    if (isLenWord(str, l)) {
        return [...oldList, newList];
    }
    return [...oldList];
};

/**
 * 求和
 * @param  {...any} list
 */
const sum = (...list) => {
    return list.reduce((a, b) => a + b, 0);
};

/**
 * 自定义模板输出到控制台
 * @param {*} str
 */
const log = (str) => (l, count) => {
    return console.log(`${l}${str}: ${count}`);
};

/**
 * 搜索单词
 * @param {*} list Array
 * @param {*} str String
 */
const search = (list, str) => {
    return list.filter(item => {
        return item[0] === str;
    });
};

/**
 * 生成指定数量的数组
 * @param {*} n
 */
const soldiers = (n) => {
    return Array(n).fill([]);
};

/**
 * 获取后缀
 * @param {*} list
 * @param {*} str
 */
const suffix = (list, str) => {
    return list.filter(item => item[0].toString().toLowerCase().endsWith(str));
};

/**
 * 获取前缀
 * @param {*} list
 * @param {*} str
 * item[0].toString() //因为单词中有true、false当作字符串处理时会报错
 */
const prefix = (list,str) => {
    return list.filter(item => item[0].toString().toLowerCase().startsWith(str))
};

/**
 * 获取包含给定字符的单词list
 * @param {*} list 
 * @param {*} str 
 */
const includes = (list,str) => {
    return list.filter(item => item[0].toString().toLowerCase().includes(str))
};

/**
 * 匹配w_ll格式的单词(_代表一个字母)
 * @param list
 * @returns {*}
 */
const w_ll =(list)=>{
    let reg = /^w\wll$/ig
    return list.filter(item => reg.test(item[0].toString().toLowerCase()))
};

/**
 * 匹配_a_格式的单词(_代表一个字母)
 * @param list
 * @returns {*}
 */
const _a_ =(list)=>{
    let reg = /^\wa\w$/ig
    return list.filter(item => reg.test(item[0].toString().toLowerCase()))
};

/**
 * 获取指定长度的单词list
 * @param {*} list 
 * @param {*} n 
 */
const lenWord = (list,n) =>{
    return list.filter(item => len(item[0].toString()) === n)
};

/**
 * 过滤单词
 * @param l
 * @returns {*}
 */
const words = (l) => {
    return l.map(item => {
        return item.filter((item, index) => {
            return index === 0;
        }).join(',');
    }).join('\n');
};

/**
 * 去除重复的单词
 * @param l
 * @returns {string}
 */
const distinct = (l) =>{
    return [...new Set(l.map(item => {
        return item.filter((item, index) => {
            return index === 0;
        }).join(',');
    }))].join('\n');
};

module.exports = {
    len,
    isLenWord,
    getWord,
    getWordList,
    sum,
    log,
    search,
    soldiers,
    suffix,
    prefix,
    includes,
    lenWord,
    distinct,
    words,
    w_ll,
    _a_
};
